package com.allianz.webapps.secondController.pay;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.common.CustomerContextHolder;
import com.allianz.webapps.dbpojo.ActualTimeDDA;
import com.allianz.webapps.service.ActualTimeDDAService;
import com.fasterxml.jackson.databind.ObjectMapper;


@Controller
@RequestMapping(value = "/main")
public class PufaBankPayController {
	
	@Autowired
	private ObjectMapper objectMapper;
	
	@Autowired
	private ActualTimeDDAService actualTimeDDAService;
	
	/**
	 * 实时代扣接口
	 * @param jsonParam
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/actualTimeDDA")
	@ResponseBody
	public Map<String,Object>  actualTimeDDA(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception{
		//调用实时代扣dda接口
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		ActualTimeDDA actualTimeDDA = objectMapper.readValue(jsonParam, ActualTimeDDA.class);
		paramMap.put("partId", actualTimeDDA.getPartId());
		paramMap.put("piAccName", actualTimeDDA.getPiAccName());
		paramMap.put("piAccountNo", actualTimeDDA.getPiAccountNo());
		paramMap.put("piBankCategoryId", actualTimeDDA.getPiBankCategoryId());
		paramMap.put("piAreaCode", actualTimeDDA.getPiAreaCode());
		paramMap.put("piContractId", actualTimeDDA.getPiContractId());
		paramMap.put("piAmount", actualTimeDDA.getPiAmount());
		Map<String,Object> maps = actualTimeDDAService.actualTimeDDA(paramMap);
		
		//把调用dda得到的参数存入数据库
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
		Map<String,Object> insertMap = new HashMap<String,Object>();
		insertMap.put("poDlBatchNo",maps.get("poDlBatchNo")!=null?maps.get("poDlBatchNo").toString():"" );
		insertMap.put("poResultCode", maps.get("poResultCode")!=null?maps.get("poResultCode").toString():"");
		insertMap.put("poResultMsg", maps.get("poResultMsg")!=null?maps.get("poResultMsg").toString():"");
		insertMap.put("payTime", this.getCurrentTime());
		insertMap.put("contractId", actualTimeDDA.getPiContractId());
		insertMap.put("retInt", maps.get("retInt")!=null?maps.get("retInt").toString():"");
		insertMap.put("times", new Date().getTime()+"");
		Assert.notNull(insertMap.get("contractId"), "未提供数据");
		actualTimeDDAService.insertDDAInfo(insertMap);
		return maps;
	}
	
	/**
	 * 获取当前时间
	 * @return
	 */
	private String getCurrentTime(){
		 Date d = new Date();
	     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	     return sdf.format(d);
	}
	
	/**
	 * 实时代扣查询接口
	 * @param jsonParam
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "queryStatus")
	@ResponseBody
	public Map<String,Object> queryStatus(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception{
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String,Object> paramMap = new  HashMap<String,Object>();
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		String strDlBatchNo = map.get("poDlBatchNo")!=null?map.get("poDlBatchNo").toString():"";
		Assert.notNull(strDlBatchNo, "支付号为空");
		Long poDlBatchNo = Long.parseLong(strDlBatchNo);//支付号
//		String poDlBatchNo = map.get("poDlBatchNo").toString();//支付号
		paramMap.put("poDlBatchNo",poDlBatchNo);
		Map<String,Object> maps = actualTimeDDAService.queryStatus(paramMap);
		return maps;
	}
	
	/**
	 * 是否在途
	 */
	@RequestMapping(value = "isUnderway")
	@ResponseBody
	public String isUnderway(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception{
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		String piContractId = map.get("piContractId")!=null?map.get("piContractId").toString():"";
		Assert.notNull(piContractId, "contractId为空");
		Long contractId = Long.parseLong(piContractId);//保单contract_id
		String retInt = actualTimeDDAService.isUnderway(contractId);
		return retInt;
	}
	
	/**
	 * 最近一次失败原因
	 */
	@RequestMapping(value = "getFailReason")
	@ResponseBody
	public String getFailReason(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception{
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		String piContractId = map.get("piContractId")!=null?map.get("piContractId").toString():"";
		Assert.notNull(piContractId, "contractId为空");
		Long contractId = Long.parseLong(piContractId);//保单contract_id
		String retInt = actualTimeDDAService.getFailReason(contractId);
		return retInt;
	}
	
	/**
	 * 最近一次失败原因
	 */
	@RequestMapping(value = "queryDDAInfo")
	@ResponseBody
	public List<Map<String,Object>> queryDDAInfo(@RequestParam(value = "jsonParam")  String jsonParam) throws Exception{
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_NEWTOUCH);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		String contractId = map.get("piContractId")!=null?map.get("piContractId").toString():"";//保单contract_id
		List<Map<String,Object>> resultMap = actualTimeDDAService.queryDDAInfo(contractId);
		return resultMap;
	}
 
}
